package com.heima.homeservicemanagementsystem.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.homeservicemanagementsystem.entity.SysUserRole;
import com.heima.homeservicemanagementsystem.module.PageParams;
import com.heima.homeservicemanagementsystem.module.Result;
import com.heima.homeservicemanagementsystem.service.SysUserRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
 
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
 

/**
 * 用户与角色关联;(sys_user_role)表控制层
 * @author : http://www.chiner.pro
 * @date : 2025-3-16
 */
@Tag(name = "用户与角色关联对象功能接口")
@RestController
@RequestMapping("/sysUserRole")
public class SysUserRoleController{

    @Resource
    private SysUserRoleService sysUserRoleService;

    @Operation(summary="分页查询")
    @PostMapping("/page")
    public Result<Page<SysUserRole>> paginQuery(@RequestBody PageParams<SysUserRole> pageParams){
        //查询参数
        SysUserRole params =pageParams.getParams();
        LambdaQueryWrapper <SysUserRole> lqw =new LambdaQueryWrapper<>();

        lqw.eq(params.getUserId()!=null,SysUserRole::getUserId,params.getUserId());

        lqw.eq(params.getRoleId()!=null,SysUserRole::getRoleId,params.getRoleId());

        return Result.success(sysUserRoleService.page(pageParams.getPage(),lqw));
    }

    /**
     * 新增数据
     *
     * @param sysUserRole 实例对象
     * @return 实例对象
     */
    @Operation(summary="新增数据")
    @PostMapping("/insert")
    public Result<Boolean> insert(@RequestBody SysUserRole sysUserRole){
        if(sysUserRoleService.save(sysUserRole)){
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 更新数据
     *
     * @param sysUserRole 实例对象
     * @return 实例对象
     */
    @Operation(summary="更新数据")
    @PutMapping("update")
    public Result<Boolean> update(@RequestBody SysUserRole sysUserRole){
        if(sysUserRoleService.updateById(sysUserRole)){
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Operation(summary="通过主键删除数据",
            parameters={
                    @Parameter(name ="id",description="主键",required =true,in=ParameterIn.PATH)
            })
    @GetMapping("/delete/{id}")
    public Result<Boolean> deleteById(@PathVariable String id){

        if(sysUserRoleService.removeById(id)){
            return Result.success();
        }
        return Result.error();
    }

}